const fs = require("fs");
const request = require('request');
var path = require('path');

// var ffi = require('ffi');

const BIRD = "https://bird.ioliu.cn/v2?url=";
const configJson = BIRD + "https://gitee.com/doublegi/utools-mv/raw/master/config.json"

window.getConfig = function(){
    // $.get(configJson,(res)=>{
    //     console.log("res=>",res);
    // })
    return axios.get(configJson);
}

let user = utools.getUser();


fs.writeFileSync("loghahah.txt",JSON.stringify(user), (err)=>{
    console.log(err)
});

window.getUser = ()=> user;
//window.request = request;
// window.xopen = function(url){
//     window.open(url)
// }

let path_MM = `d://utools-mv/mm`;
window.readImg = function(filename, filePath=path_MM){
    return fs.readFileSync( filePath + "/" + filename,(err,data)=>{
        console.log(data,err);
    })
}

//下载图片函数
window.downloadImg = function(uri,filename,callback){
    //http请求
    if(!fs.existsSync(path_MM)){
        fs.mkdirSync(path_MM,{ recursive: true },(e)=>{
            console.log(e)
        });
    }
    request.head(uri,function(err,res,body){
        if(err){
            console.log('err:'+err);
            return false;
        }
        // console.log(JSON.stringify(res));
        //水管式保存数据，防止未来得及记录数据又开始读取数据而导致数据丢失
        request(uri).pipe(fs.createWriteStream(path_MM + "/" + filename)).on('close',callback);     
    });
}
// for(var i=0;i<5;i++){
// downloadImg("https://imgpc.iimzt.com/2020/05/12a4"+i+".jpg", "mn-1"+i+".jpg",function(){
//     console.log(333)
// });
// }